﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace Framework.WebmasterTool.Orders
{
    public partial class OrderAnswers : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadOrderAnswerDetails();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.Contact.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void LoadOrderAnswerDetails()
        {
            if (Request.QueryString["OrderID"] != null)
            {
                int orderID;
                int.TryParse(Request.QueryString["OrderID"], out orderID);
                udv_Orders order = entity.udv_Orders.Where(od => od.OrderID == orderID).FirstOrDefault();
                if (order != null)
                {
                    var orderAnswerDetails = entity.udv_OrderAnswers.Where(od => od.udv_Orders.OrderID == orderID).OrderByDescending(cs => cs.AddedDate).ToList();
                    grvOrderAnswers.DataSource = orderAnswerDetails;
                    grvOrderAnswers.DataBind();
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void grvOrderAnswers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvOrderAnswers.PageIndex = e.NewPageIndex;
            LoadOrderAnswerDetails();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grvOrderAnswers.Rows)
            {
                bool isChecked = ((HtmlInputCheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int orderID = int.Parse(((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].Trim());
                    udv_OrderAnswers orderAnswerDelete = entity.udv_OrderAnswers.Where(cs => cs.udv_Orders.OrderID == orderID).FirstOrDefault();
                    if (orderAnswerDelete != null)
                    {
                        //create db log
                        orderAnswerDelete.udv_OrdersReference.Load();
                        CreateDBLog(null, null, TypeAction.Delete, "Xoá trả lời đặt hàng: " + orderAnswerDelete.udv_Orders.Receiver + " - " + string.Format("{0:dd/MM/yyyy HH:mm:ss}", orderAnswerDelete.udv_Orders.AddedDate), orderAnswerDelete.AnswerTitle, orderAnswerDelete.AnswerContent, "", "",
                            "", "", "", "");

                        entity.DeleteObject(orderAnswerDelete);
                        entity.SaveChanges();
                    }
                }
            }
            LoadOrderAnswerDetails();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["OrderID"] != null)
            {
                int orderID;
                int.TryParse(Request.QueryString["OrderID"], out orderID);
                udv_Orders contactDetails = entity.udv_Orders.Where(od => od.OrderID == orderID).FirstOrDefault();
                if (contactDetails != null)
                {
                    Response.Redirect("OrderDetails.aspx?OrderID=" + orderID);
                }
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}